﻿using Game.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Game.DeviceHandler
{
        delegate void UpdateListDelegate(int index);
    delegate void UpdateText(string str);
    class DeviceDm
    {

        protected Thread workThread; //主线程
        protected Thread checkThread;//监控线程
        protected int[] m_minArr = new int[4];
        protected dmsoft dm;
        protected dmsoft dmCheck;
        public int hWnd { get; set; }
        protected bool isAbort = true;//
        public UpdateListDelegate updateListDelegate;
        public UpdateText updateText;
        public void DoJob(object index)
        {
            

            string ret = "";
            Type t = this.GetType();
            BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
            MethodInfo m = this.GetType().GetMethod("TaskBoss", flag);
            if (m != null)
            {
                try
                {
                    ret = m.Invoke(this, null).ToString();
                }
                catch (Exception)
                {
                }
            }
        }
        public DeviceDm(int hWnd)
        {
            this.hWnd = hWnd;
            this.dm = Game.Common.Util.CreateDM(hWnd);
        }
        /// 启动(指定账号) 指定账号<0 启动全部
        /// </summary>
        /// <param name="index"></param>
        public void Start(int index)
        {
            workThread = new Thread(new ParameterizedThreadStart(DoJob));
            workThread.SetApartmentState(ApartmentState.STA);
            workThread.Start(index);

            
            checkThread = new Thread(new ParameterizedThreadStart(CheckOnline));
            checkThread.SetApartmentState(ApartmentState.STA);
            checkThread.Start(index);
        }

        /// <summary>
        /// 全局延时
        /// </summary>
        /// <param name="time">时间</param>
        protected void MainDelay(int time)
        {

            while (true)
            {
                if (this.isAbort)
                {
                    break;
                }
                Thread.Sleep(1);
            }
            Thread.Sleep(time);
        }
        public bool getThreadState()
        {
            return isAbort;
        }
        /// <summary>
        /// 检测线程
        /// </summary>
        /// <param name="index"></param>
        private void CheckOnline(object index)
        {

            dmCheck = Util.CreateDM(this.hWnd);
            int x, y;
            while (true)
            {
                
                 dmCheck.FindPic(0, 0, m_minArr[2] + 1, m_minArr[3] + 1, "fuwen.bmp|fuwen1.bmp", "050505", 0.9, 0, out x, out y);
                //dmCheck.FindPic(0, 0, m_minArr[2]+1, m_minArr[3]+1, "fuwen.bmp|fuwen1.bmp|fuwen2.bmp", "050505", 0.9, 0, out x, out y);

                Common.Common.str = dmCheck.GetColor(500, 733) + "|" + dmCheck.GetColor(500, 729);
                if (x > -1)
                {
                    isAbort = false;
                }
                if (dmCheck.CmpColor(500, 733, "4f4d52", 0.9f) == 0)
                {
                    dmCheck.KeyPress(35);
                    MainDelay(50);
                    Console.WriteLine("补血");
                }
                if (dmCheck.CmpColor(470, 733, "4f4d52", 0.9f) == 0)
                {
                    dmCheck.KeyPress(36);
                    MainDelay(500);
                    Console.WriteLine("补血");
                }
            }
            // lock (Common.Gobal_Lock) 锁
            ////Common.Common.Gobal_Lock
            //if (lw
            //{


            //    lwCheck.delay(300)
            //lwCheck.KeyPress(35)
            //traceprint("补血")
            //lwCheck.delay(1000)
            //}

        }

        /// <summary>
        /// 线程暂停
        /// </summary>
        /// <param name="index"></param>
        public void Stop(int index, bool bflag)
        {
            isAbort = bflag;

            //throw new NotImplementedException();
        }
        public void Stop1()
        {
            workThread.Abort();
            checkThread.Abort();
        }

        /// <summary>
        /// 查找小地图坐标范围
        /// </summary>
        protected void findMinMap()
        {
            int x = 0, y = 0;
            this.dm.FindPic(0, 0, 400, 50, "word.bmp|word1.bmp", "050505", 0.90f, 0,out x,out y);
            m_minArr[2] = x + 40;//x1
            this.dm.FindPic(0, 0, 400, 50, "npc.bmp", "050505", 0.90f, 0, out x, out y);
            if (x > 0)
            {
                m_minArr[2] = x + 40;//x1
            }
            m_minArr[1] = y - 5;//y
            this.dm.FindPic(0, 0, 200, 180, "minMapLeft.bmp", "202020", 0.9f, 0,out x, out y);
            m_minArr[0] = x;//x
            m_minArr[3] = y + 30;//y1
        }

        /// <summary>
        /// 查找当前角色坐标
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        protected void findRolePoint(out int x, out int y)
        {
            
            dm.FindPic(0, 0, m_minArr[2], m_minArr[3], "localTag.bmp|localTag1.bmp|localTag2.bmp|localTag3.bmp|localTag4.bmp", "050505", 0.85f, 0,out x,out y);

        }

        /// <summary>
        /// 查找地图标志
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        protected List<string> findMapPoint()
        {
            //this.dm.FindPicEx(0, 0, m_minArr[2], m_minArr[3], "tu1.bmp", "202020", 0.9f, 0);
            string str = this.dm.FindPicEx(0, 0, m_minArr[2], m_minArr[3], "tu1.bmp", "202020", 0.9f, 0);
            return str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
        }

        /// <summary>
        /// 往上跳
        /// </summary>
        protected void moveJump()
        {

            lock (Common.Common.Gobal_Lock)
            {
                this.dm.KeyDown(18);
                MainDelay(Util.rnd(30, 80));
                this.dm.KeyUp(18);
                MainDelay(Util.rnd(30, 88));
                this.dm.KeyDown(38);
                MainDelay(Util.rnd(50, 88));
                this.dm.KeyUp(38);
                MainDelay(Util.rnd(50, 98));
                this.dm.KeyDown(38);
                MainDelay(Util.rnd(500, 888));
                this.dm.KeyUp(38);
                MainDelay(500);
            }
        }
        protected void moveJumpAttack(int mode1, int mode2)
        {
            this.dm.KeyDown(mode1);
            this.dm.delay(200);
            this.dm.KeyPress(18);
            this.dm.delay(300);
            this.dm.KeyDown(18);
            this.dm.delay(300);
            this.dm.KeyUp(18);
            this.dm.delay(300);
            this.dm.KeyUp(mode1);
            this.dm.delay(500);
            this.dm.KeyPress(65);
            this.dm.delay(Util.rnd(500, 1000));
            this.dm.KeyPress(mode2);
            this.dm.delay(Util.rnd(500, 1000));
            this.dm.KeyPress(65);
        }
        /// <summary>
        /// 移动攻击
        /// </summary>
        /// <param name="mode"></param>
        protected void moveAttack(int mode)
        {
            lock (Common.Common.Gobal_Lock)
            {
                this.dm.KeyDown(mode);
                MainDelay(Util.rnd(300, 500));
                this.dm.KeyDown(18);
                MainDelay(Util.rnd(100, 150));
                this.dm.KeyUp(18);
                MainDelay(Util.rnd(100, 150));
                this.dm.KeyDown(65);
                MainDelay(Util.rnd(100, 150));
                this.dm.KeyUp(65);
                MainDelay(Util.rnd(300, 500));
                this.dm.KeyUp(mode);
                MainDelay(Util.rnd(100, 150));
                //MainDelay(Util.rnd(300, 500));
            }
        }

        /// <summary>
        /// 根据方向跳
        /// </summary>
        /// <param name="mode"></param>
        protected void moveJump(int mode)
        {
            lock (Common.Common.Gobal_Lock)
            {
                this.dm.KeyDown(mode);
                MainDelay(Util.rnd(300, 500));
                this.dm.KeyDown(18);
                MainDelay(Util.rnd(100, 150));
                this.dm.KeyUp(18);
                MainDelay(Util.rnd(100, 150));
                this.dm.KeyDown(18);
                MainDelay(Util.rnd(100, 150));
                this.dm.KeyUp(18);
                MainDelay(Util.rnd(300, 500));
                this.dm.KeyUp(mode);
            }
        }


        protected int findmaster()
        {
            int nRoleX, nRoleY;
            findRolePoint(out nRoleX, out nRoleY);
            string str = "1024,491,35,29,50,4499dd,80,350";
            string[] sarr = str.Split(',');
            string ss;
            int min, max;
            string[] arr1, arr2;
            if (nRoleX < int.Parse(sarr[6]))
            {
                //查找大于sarr[1]的怪物数据
                ss = dm.FindColorBlockEx(0, 0, int.Parse(sarr[0]), int.Parse(sarr[7]), sarr[5], 0.99f, int.Parse(sarr[4]), int.Parse(sarr[2]), int.Parse(sarr[3]));

            }
            else
            {
                //查找小于sarr[1]的怪物数据
                ss = dm.FindColorBlockEx(0, int.Parse(sarr[7]), int.Parse(sarr[0]), int.Parse(sarr[7]) + 200, sarr[5], 0.99f, int.Parse(sarr[4]), int.Parse(sarr[2]), int.Parse(sarr[3]));
                Console.WriteLine("down");
            }
            int a = 0, b = 0;
            arr1 = ss.Split('|');

            for (int i = 0; i < arr1.Length; i++)
            {
                arr2 = arr1[i].Split(',');
                if (int.Parse(arr2[2]) > 612)
                {
                    if (a < int.Parse(arr2[2]) - 612)
                    {
                        a = int.Parse(arr2[2]) - 612;
                    }
                }
                else
                {

                    if (b < 612 - int.Parse(arr2[2]))
                    {
                        b = 612 - int.Parse(arr2[2]);
                    }
                }
            }

            if (a < b) //a左 b 右    0左 1 右 2 直接
            {
                if (a <= 50)
                {
                    return 2;
                }
                return 1;//右
            }
            else
            {
                if (b <= 50)
                {
                    return 2;
                }
                return 0;//左
            }
            //return ss;
            //Console.WriteLine(ss);


            // "任务坐标y,怪物坐标y"
            //查询当前地图的坐标
            //如果坐标< 则
            //如果坐标> 则



            //Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
            //List<string> list = new List<string>();
            //list.Add("0,0,1024,300,000000,400");
            //list.Add("0,0,1024,300,000000,400");
            //dict.Add("0", list);

            //"高度1|高度2,颜色,数量"
            //if (x < 88)
            //{

            //}else
            //{

            //}

        }

        protected bool moveRole(int endX,int juli=0)
        {
            //取当前坐标
            int x = 0, y = 0;
            int delay = 100;
            for (int i = 0; i < 100; i++)
            {
                MainDelay(300);
                findRolePoint(out x, out y);
                //取用户坐标
                Console.WriteLine("当前坐标{0}坐标差1={0}", x, System.Math.Abs(endX - x));
                if (System.Math.Abs(endX - x) <= juli)
                {
                    return true;
                }
                if (x > endX)//往左移动
                {
                    dm.KeyDown(37);
                    if (System.Math.Abs(endX - x) <= 5)
                    {
                        Console.WriteLine("当前坐标差1={0}", System.Math.Abs(endX - x));
                        MainDelay(delay);
                    }
                    else
                    {
                        MainDelay(Util.rnd(1000, 1500));
                    }
                    dm.KeyUp(37);
                    Console.WriteLine("往左移动");
                }
                if (x < endX)//往右移动
                {
                    dm.KeyDown(39);
                    if (System.Math.Abs(endX - x) <= 5)
                    {
                        Console.WriteLine("当前坐标差2={0}", System.Math.Abs(endX - x));
                        MainDelay(delay);
                    }
                    else
                    {
                        MainDelay(Util.rnd(1000, 1500));
                    }
                    dm.KeyUp(39);
                    Console.WriteLine("往右移动");
                }

            }
            return false;
        }
    }


}
